/*=======================================================================================
	AverageResponse.do
	
		Uses the Personal Finance Website (PFW) data to analyze the response of 
		consumption to the APFD payments
		
	Author: Lorenz Kueng, June 2015
=========================================================================================*/

cap log close PFW_05
log using "$homedir/log-files/PFW_05_$date.log", text replace name(PFW_05)


*** Load data

use "$homedir/data/stata/PFW_quarterly.dta", clear 

	generate date = Quarter
	format   date %tq
	
	lab var APFD "PFD payments" // APFD payments indentified via direct deposit entry in early October

	xtset userid date

	
*** Create additional variables

	* controls
	foreach var in residentialstatus marriagestatus {
		display _n(1) "`var'"
		cap drop `var'_
		encode   `var', generate(`var'_)
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	foreach var in age edu profession homezipcode {
		display _n(1) "`var'"
		cap drop `var'_
		generate `var'_ = `var'
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	
	cap drop _temp
	gen D_incomeQ = D.incomeQ
	bysort userid (Quarter): egen _temp = mean(D_incomeQ)
	replace D_incomeQ = _temp if missing(D_incomeQ)==1
	drop _temp*
	
	foreach var in incomeQ D_incomeQ incomeY liquid_asset1 {
		winsor `var', g(`var'W) p(0.01)
		sum `var' `var'W, d
	}
	
	** total expenditures as a measure of permanent income (excluding uncategorized transactions, ie only nondurables, services, durables)
	cap drop totexp1_annual
	bysort userid: egen totexp1_annual = mean(totexp1) 
	replace totexp1_annual =  totexp1_annual*4
	replace totexp1_annual = -totexp1_annual
	lab var totexp1_annual "annualized total expenditures (totexp1)"
	summarize totexp1_annual, detail
	winsor    totexp1_annual, p(0.01) generate(totexp1_annualW)
	summarize totexp1_annual totexp1_annualW, detail
	
	
	
*================================	
*** Run regressions
*===============================

*** A. Average MPC

foreach depvar in nondur_serv {

		display _n(5) "*** `depvar' ****"
		
		global depvar = "`depvar'"

		global file "$homedir/results/tables/Table2a_D${depvar}_PFWquarterly_${date}"

		capture: rm "${file}.xls"
		capture: rm "${file}.txt"
		
		
		*-----------------------------------------------------------
		* Winsorize dependent variable at 1%
		*-----------------------------------------------------------
		
		xtset userid date
		
		* make expenditures positive

		replace ${depvar} = -${depvar}

		* save a copy of the original variable

		cap drop original
		generate original = ${depvar}
		
		* winsorize at 1% (conditional on non-zero observations for disaggregated categories)
		
		summarize ${depvar}, detail
		summarize ${depvar} if ${depvar}!=0, detail

		count if ${depvar}==0 
		
		if `r(N)'<5000 {
		
			cap drop temp
			winsor ${depvar}, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}
		else { // winsorize conditional on positive entries (non-zeros) for disaggregated variables
		
			cap drop temp
			winsor ${depvar} if ${depvar}!=0, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}	
		summarize ${depvar}, detail
		summarize ${depvar} if ${depvar}!=0, detail

		* make all variables positive (i.e., expenditures)
			
		replace $depvar = abs(${depvar}) 
		summarize ${depvar}, detail

		xtset userid date

		
		*** Impose same sample in all regressions

			reghdfe ${depvar} APFD ///
					family_sizeImp ///
					liquid_asset1 ///
					incomeY ///
					D_incomeQ ///
				if APFDid_annual!=. /// NOTE: APFDid_annual!=. conditions on Alaskans for whom we know they received a dividend via direct deposit in early October
				, absorb(date userid marriagestatus_ residentialstatus_ age_ edu_ profession_ homezipcode_) vce(cluster userid)

			cap drop _e_sample
			generate _e_sample = e(sample)


		*-----------------------------------------------------------
		* Regressions
		*-----------------------------------------------------------
		
		reghdfe D.${depvar} APFD /// 
			if _e_sample==1 ///
			, absorb(date Alaska family_sizeImp) vce(cluster userid)
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("Main effects") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", --, ///
							" - Current income - level and change", --, ///
							" - Permanent income", --, ///
							" - Household characteristics", --, ///
							" - Household FE", --, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )

					
		reghdfe D.${depvar} APFD /// 
			liquid_asset1 D_incomeQ incomeY /// liquidity constraints: low liquid asssets (liquid_asset1), current income 'shocks' (D_incomeQ), and accumulated income shocks (incomeY)
			if _e_sample==1 ///
			, absorb(date Alaska family_sizeImp) vce(cluster userid)			

				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("D_Income & Liquidity") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", --, ///
							" - Household characteristics", --, ///
							" - Household FE", --, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )


		reghdfe D.${depvar} APFD /// 
			liquid_asset1 D_incomeQ incomeY /// liquidit constraints: low liquid asssets (liquid_asset1), current income 'shocks' (D_incomeQ), and accumulated income shocks (incomeY)
			totexp1_annual /// being poor (low permanent income)
			if _e_sample==1 ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)			

				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("D_Income & Liquidity") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes, ///
							" - Household FE", --, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )

					
		reghdfe ${depvar} APFD ///
			liquid_asset1 D_incomeQ incomeY ///
			/// totexp1_annual /// absorbed by HH FEs
			if _e_sample==1 ///
			, absorb(date userid family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("FE estimator") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", --, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", --, ///
							" - Household characteristics", Yes, ///
							" - Household FE", Yes, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )


		reghdfe D.${depvar} APFD ///
			liquid_asset1 D_incomeQ incomeY ///
			/// totexp1_annual /// absorbed by HH FEs
			i.date##i.Alaska ///
			if _e_sample==1 ///
			, absorb(date userid family_sizeImp marriagestatus_ residentialstatus_ age_ edu_ profession_ homezipcode_) vce(cluster userid)
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("State x time FE") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", --, /// 
							" - State FE", --, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", --, ///
							" - Household characteristics", Yes, ///
							" - Household FE", Yes, /// 
							" - State x time FE", Yes /// 
					) ///
					keep( APFD )


		
		reghdfe D.${depvar} APFD ///
			liquid_asset1 D_incomeQ incomeY ///
			/// totexp1_annual /// absorbed by HH FEs
			if _e_sample==1 & Alaska==1 ///
			, absorb(date userid family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)			
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("Alaskans only") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", --, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", --, ///
							" - Household characteristics", Yes, ///
							" - Household FE", Yes, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )
					
					
					
		*** State x time x income quintile FEs for Referee 1

			** Create income quintiles
			
				** income per household-equivalent
				generate incomeYeq = incomeY / equivalence
				lab var  incomeYeq  "annual after-tax income per household equivalence"
				
				** winsor
				winsor incomeYeq, p(0.01) generate(_temp)
				replace incomeYeq = _temp
				drop _temp
				
				** annualize
				bysort userid: egen _temp = mean(incomeYeq)
				replace incomeYeq = _temp
				drop _temp
			
				** compute quintiles 	
				egen incomeY_quintile = xtile(incomeYeq), n(5) by(year Alaska)
				
			
		reghdfe D.${depvar} APFD ///
			liquid_asset1 D_incomeQ incomeY ///
			/// totexp1_annual /// absorbed by HH FEs
			i.date##i.Alaska##i.incomeY_quintile ///
			if _e_sample==1 ///
			, absorb(date userid family_sizeImp marriagestatus_ residentialstatus_ age_ edu_ profession_ homezipcode_) vce(cluster userid)
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("State x time x income quintile FE") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", --, /// 
							" - State FE", --, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", --, ///
							" - Household characteristics", Yes, ///
							" - Household FE", Yes, /// 
							" - State x time FE", --, /// 
							" - State x time x income quintile FE", Yes /// 
					) ///
					keep( APFD )

	cap rm "${file}.txt"
}
*/



*** B. By category (spending vs. consumption, cash withdrawals, total expenditures) 

	generate food_away = cat707 + cat706 + cat704 + cat708 // food away from home: restaurants (707), fast food (706), coffee shops (704), alcohol & bars (708)
	generate food_home = cat701 // food at home: groceries (701)
	generate kids_nds  = cat602 + cat609 + cat6 // nondurable kid's expenditures: babysitter (602), kid's activities (609), and kids (6)

	global file "$homedir/results/tables/Table2b_Disaggregated_and_Total_Expenditures_PFWquarterly_${date}"
	cap rm "${file}.xls"
	cap rm "${file}.txt"

	foreach depvar of varlist ///
						food_home food_away kids_nds /// 
						cash_withdrawals ///
						totexp5 /// 
					{

		display _n(5) "*** `depvar' ****"
		
		global depvar = "`depvar'"
		
		
		*-----------------------------------------------------------
		* Winsorize dependent variable at 1%
		*-----------------------------------------------------------
		
		xtset userid date
		
		* make expenditures positive

		replace ${depvar} = -${depvar}

		* save a copy of the original variable

		cap drop original
		generate original = ${depvar}
		
		* winsorize at 1% (conditional on non-zero observations for disaggregated categories)
		
		summarize ${depvar}, detail
		summarize ${depvar} if ${depvar}!=0, detail

		count if ${depvar}==0 
		
		if `r(N)'<5000 {
		
			cap drop temp
			winsor ${depvar}, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}
		else { // winsorize conditional on positive entries (non-zeros) for disaggregated variables
		
			cap drop temp
			winsor ${depvar} if ${depvar}!=0, generate(temp) p(0.01) 
			replace temp = 0 if temp==.
			replace ${depvar} = temp
		}	
		summarize ${depvar}, detail
		summarize ${depvar} if ${depvar}!=0, detail

		* make all variables positive (i.e., expenditures)
			
		replace $depvar = abs(${depvar}) 
		summarize ${depvar}, detail

		xtset userid date
		
		
		*-----------------------------------------------------------
		* Regressions
		*-----------------------------------------------------------	
	
		reghdfe ${depvar} APFD /// 
			liquid_asset1 D_incomeQ incomeY ///
			totexp1_annual ///
			if _e_sample==1 ///
			, absorb(date userid family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("${depvar},FE estimator") ///
					addtext( ///
							" - Time FE", Yes, /// 
							" - Family size FE", Yes, ///
							" - State FE", Yes, /// 
							" - Income", Yes, ///
							" - Liquid assets", Yes, ///
							" - Household characteristics", Yes, ///
							" - Household FE", Yes, /// 
							" - State x time FE", -- /// 
					) ///
					keep( APFD )
}
cap rm "${file}.txt"


log close PFW_05
